<?php
    class Image_m extends Model {
        function Image_m(){
            parent::Model();
            $this->load->database();
        }

        function insert_image_album($data){
            $this->db->insert('doggy_images', $data);
        }
        function insert_image_comment($data){
            $this->db->insert('doggy_wall_messages', $data);
        }

        function get_max_image_id($data){
            $this->db->select('MAX(doggy_images.id) AS max_id');
            $this->db->from('doggy_images');
            $query = $this->db->get();
            return $query->result_array();
        }
        function get_image_by_id($id){
            $this->db->select();
            $this->db->from('doggy_images');
            $this->db->where('id',$id);
            return $this->db->get();
        }
        function get_image_name($data){
            $this->db->select('image');
            $this->db->from('doggy_images');
            $this->db->where('id', $data);
            $query = $this->db->get();
            return $query->result_array();
        }
        function get_recent_images($account_id,$limit){
            $this->db->select('doggy_images.id,doggy_images.thumbs,doggy_images.image');
            $this->db->from('doggy_images');
            $this->db->where('doggy_images.account_id',$account_id);
            $this->db->limit($limit);
            $query = $this->db->get();
            return $query->result_array();
        }

        function count_image_shared($id){
            $this->db->select();
            $this->db->from('doggy_wall_messages');
            $this->db->where(array('posted_by'=>$id));
            $this->db->order_by('posted_time','DESC');
            return count($this->db->get()->result_array());
        }
        function get_image_shared($id,$per_page,$off_set){
            $this->db->select();
            $this->db->from('doggy_wall_messages');
            $this->db->where(array('posted_by'=>$id));
            $this->db->order_by('posted_time','DESC');
            $this->db->limit($per_page,$off_set);
            return $this->db->get()->result_array();
        }
        function get_image_private($id,$per_page,$off_set){
            $this->db->select();
            $this->db->from('doggy_images');
            $this->db->where('doggy_images.image NOT IN (SELECT doggy_wall_messages.image FROM doggy_wall_messages)');
            $this->db->order_by('upload_time ','DESC');
            $this->db->where(array('account_id'=>$id));
            $this->db->limit($per_page,$off_set);
            return $this->db->get()->result_array();
        }
        function count_image_private($id){
            $this->db->select();
            $this->db->from('doggy_images');
            $this->db->where('doggy_images.image NOT IN (SELECT doggy_wall_messages.image FROM doggy_wall_messages)');
            $this->db->order_by('upload_time ','DESC');
            $this->db->where(array('account_id'=>$id));
            return count($this->db->get()->result_array());
        }
        function delete_shared_picture($id){
          return $this->db->delete('doggy_wall_messages',array('id'=>$id));
        }
        function delete_private_images($id){
            return $this->db->delete('doggy_images',array('id'=>$id));
        }
        function get_shared_file_name($id){
            return $this->db->select('doggy_wall_messages.image')
            ->from('doggy_wall_messages')->where(array('id'=>$id))
            ->get();
        }
        function get_private_file_name($id){
            $this->db->select();
            $this->db->from('doggy_images');
            $this->db->where('doggy_images.image NOT IN (SELECT doggy_wall_messages.image FROM doggy_wall_messages)');
            $this->db->where(array('id'=>$id));
            return $this->db->get();
        }
        function count_all_images($id){
            $this->db->select();
            $this->db->from('doggy_images');
            $this->db->where(array('account_id'=>$id));
            $this->db->order_by('upload_time','DESC');
            return count($this->db->get()->result_array());
        }

        function get_my_galary($id,$per_page,$off_set){
            $this->db->select();
            $this->db->from('doggy_images');
            $this->db->where(array('account_id'=>$id));
            $this->db->order_by('upload_time','DESC');
            $this->db->limit($per_page,$off_set);
            return $this->db->get()->result_array();
        }
        function add_album($data){
            return $this->db->insert('doggy_albums',$data);
        }

        function get_image_by_album_title($title,$account_id,$per_page,$off_set){
            $this->db->select('
            doggy_images.id,
            doggy_images.thumbs,
            doggy_images.image,
            doggy_images.album_id,
            doggy_images.account_id,
            doggy_images.upload_time,
            doggy_albums.id,
            doggy_albums.title
            ');
            $this->db->from('doggy_images,doggy_albums');
            $this->db->where('doggy_images.album_id = doggy_albums.id');
            $this->db->where(array('doggy_images.account_id'=>$account_id));
            $this->db->where('doggy_images.album_id',$title);
            $this->db->order_by('doggy_images.upload_time','DESC');
            $this->db->limit($per_page,$off_set);
            return $this->db->get()->result_array();

        }

        function count_get_image_by_title($title,$account_id)
        {
            $this->db->select('
            doggy_images.id,
            doggy_images.thumbs,
            doggy_images.image,
            doggy_images.album_id,
            doggy_images.account_id,
            doggy_images.upload_time,
            doggy_albums.id,
            doggy_albums.title
            ');
            $this->db->from('doggy_images,doggy_albums');
            $this->db->where('doggy_images.album_id = doggy_albums.id');
            $this->db->where(array('doggy_images.account_id'=>$account_id));
            $this->db->where('doggy_images.album_id',$title);
            $this->db->order_by('upload_time','DESC');
            return count($this->db->get()->result_array());
        }

    }
?>